package com.team.abs.service;

import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;

public  class cache {
	public  static LoadingCache<String, String> getCache(){
		
		 CacheLoader<String, String> loader = new CacheLoader<String, String> () {
	          public String load(String key) throws Exception {
	              Thread.sleep(1000);
	              if("key".equals(key)) return null;
	              System.out.println(key + " is loaded from a cacheLoader!");
	              return key + "'s value";
	          }
	      };

	      RemovalListener<String, String> removalListener = new RemovalListener<String, String>() {
	          public void onRemoval(RemovalNotification<String, String> removal) {
	              System.out.println("[" + removal.getKey() + ":" + removal.getValue() + "] is evicted!");
	          }
	      };
	      
		 LoadingCache<String, String> testCache = CacheBuilder.newBuilder()
	             .maximumSize(1000)
	             .expireAfterWrite(5, TimeUnit.MINUTES)
	             .removalListener(removalListener)
	             .build(loader);

		return testCache;
	}
}
